package com.krain.corelibrary.widget;

import android.content.Context;
import android.os.CountDownTimer;
import android.util.AttributeSet;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.krain.corelibrary.R;


public class TimerButton extends Button implements OnClickListener {

    private static final int DEFAULT_TIME     = 60;
    private static final int DEFAULT_INTERVAL = 1000;

    private int              time             = DEFAULT_TIME;
    private int              interval         = DEFAULT_INTERVAL;
    private OnTimerListener  onTimerListener;
    private int              stringInitId     = -1;
    private String           text;
    private boolean          isCanStart       = true;
    private CountDownTimer            timer;

    public interface OnTimerListener {
        void onClick();

        void onTiming(int remainTime);

        void onFinish();
    }

    public void setOnTimerListener(OnTimerListener l) {
        this.onTimerListener = l;
    }

    public TimerButton(Context context) {
        super(context);
        this.setOnClickListener(this);
    }

    public TimerButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.setOnClickListener(this);
    }

    public TimerButton(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        this.setOnClickListener(this);
    }

    /**
     * 倒计时时间，单位：秒。默认60秒
     *
     * @param time
     */
    public void setTime(int time) {
        this.time = time;
    }

    public void setInterval(int interval) {
        this.interval = interval;
    }

    public void setIsCanStart(boolean isCanStart) {
        this.isCanStart = isCanStart;
    }

    public void reset() {
        if (timer == null) return;
        timer.cancel();
        timer.onFinish();
    }

    @Override
    public void onClick(View v) {
        if (isCanStart) {
            this.setEnabled(false);
            startTimer();
            if (onTimerListener != null)
                onTimerListener.onClick();
        }
    }

    /**
     * 进入页面默认获取验证码的情况
     */
    public void autoStart(){
        this.setEnabled(false);
        startTimer();
        if (onTimerListener != null)
            onTimerListener.onClick();
    }

    /**
     * 开始计时
     */
    private void startTimer() {
        isCanStart = false;
        setTextColor(getResources().getColor(R.color.default_grey_color));
        if (timer == null) {
            timer = new CountDownTimer(time * 1000, interval) {
                @Override
                public void onTick(long millisUntilFinished) {
                    int remainTime = (int) (millisUntilFinished / 1000);
                    setText(getResources().getString(R.string.timer_tempalte, remainTime));
                    if (onTimerListener != null)
                        onTimerListener.onTiming(remainTime);
                }

                @Override
                public void onFinish() {
                    isCanStart = true;
                    setTextColor(getResources().getColor(R.color.default_orange_color));
                    setEnabled(true);
                    if (stringInitId != -1) {
                        setText(getResources().getString(stringInitId));
                    } else {
                        setText(getResources().getString(R.string.timer_verification_code_get));
                    }
                }
            };
        }
        timer.start();
    }

    public void setNormalDisplay(int stringInitId) {
        this.stringInitId = stringInitId;
    }


    public boolean isCanStart() {
        return isCanStart;
    }
}
